package org.jeecg;

import java.util.Map;


import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;


import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.common.collect.Maps;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ZhiYuanZhe {
	
	public static void main(String[] args) {
		get_list(1349);
	}
	
	/**
	 * 邯郸新闻网
	 */
	public static void get_list(int page){
//		Host: www.chinavolunteer.cn
//		Connection: keep-alive
//		Upgrade-Insecure-Requests: 1
//		User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
//		Sec-Fetch-User: ?1
//		Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
//		Sec-Fetch-Site: same-origin
//		Sec-Fetch-Mode: navigate
//		Referer: https://www.chinavolunteer.cn/app/mz/vol.list.php
//		Accept-Encoding: gzip, deflate, br
//		Accept-Language: zh-CN,zh;q=0.9
//		Cookie: PHPSESSID=7mh91nee1do3mq1osu7vktjun3; PHPSESSID=7mh91nee1do3mq1osu7vktjun3; Hm_lvt_96c55a3847063dc0f3d6d32b55654297=1591148281,1591148481; Hm_lpvt_96c55a3847063dc0f3d6d32b55654297=1591149997

		
		//整个html内容
		Document doc;
		int errcount = 0;
		try {
			
			trustEveryone();
			
			Connection conn = Jsoup.connect("https://www.chinavolunteer.cn/app/mz/vol.list.php?p=2").timeout(5000);
			//Connection conn = Jsoup.connect("https://www.chinavolunteer.cn/app/mz/vol.list.php?p=2").timeout(5000);
			conn.header("Connection", "keep-alive");
			conn.header("Host", "www.chinavolunteer.cn");
			conn.header("Upgrade-Insecure-Requests", "1");
			conn.header("Sec-Fetch-User", "?1");
			conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
			conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
			conn.header("Accept-Encoding", "gzip, deflate, br");
			conn.header("Accept-Language", "zh-CN,zh;q=0.9");
			conn.header("Referer", "https://www.chinavolunteer.cn/app/mz/vol.list.php?p=2");
			conn.header("Sec-Fetch-Site", "same-origin");
			conn.header("Sec-Fetch-Mode", "navigate");
			conn.header("Sec-Fetch-Site", "same-origin");
			conn.header("Cookie", "PHPSESSID=7mh91nee1do3mq1osu7vktjun3; Hm_lvt_96c55a3847063dc0f3d6d32b55654297=1591148281,1591148481; PHPSESSID=7mh91nee1do3mq1osu7vktjun3; Hm_lpvt_96c55a3847063dc0f3d6d32b55654297=1591165813");


		
			
//			Connection conn = Jsoup.connect("https://www.chinavolunteer.cn/app/mz/vol.list.php?p=2").timeout(5000);
//			conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
//			conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
//			conn.header("Accept-Encoding", "gzip, deflate, br");
//			conn.header("Accept-Language", "zh-CN,zh;q=0.9");
			
//			Map<String, String> cookies = Maps.newConcurrentMap();
//			cookies.put("PHPSESSID", "7mh91nee1do3mq1osu7vktjun3");
//			cookies.put("PHPSESSID", "7mh91nee1do3mq1osu7vktjun3");
//			cookies.put("Hm_lvt_96c55a3847063dc0f3d6d32b55654297", "1591148281,1591148481");
//			cookies.put("Hm_lpvt_96c55a3847063dc0f3d6d32b55654297", "1591150914");
//			conn.cookies(cookies);
			
			
			doc = conn.get();
			System.out.println(doc.html());
			String name = doc.getElementsByTag("title").text();
			log.info("*******************************************第"+page+"页("+name+")********************************************");
			
			Elements tablelist = doc.select("div#result_list table");
			if(!tablelist.isEmpty()) {
				Elements list = tablelist.get(1).getElementsByTag("td");
				for (Element info : list) {
					System.out.println(info.select("a").html());
					String title = info.select("a").first().text();
					String url = info.select("a").first().attr("href");
					String time = "";
				
					
					log.info(title);
					log.info(url);
					log.info(time);
					
					log.info("----------------------------------------重复次数"+errcount+"--------------------------------------------------");

				}
			}
			
		
			//查询分页列表
			page++;
			if(!tablelist.isEmpty() && errcount < 8) {
				//get_list(keyword, page, type);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 设置连接超时时间 
	}
	
	

/**
	 * 信任任何站点，实现https页面的正常访问
	 * 
	 */
	
	public static void trustEveryone() {
        try {  
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;  
                }
            });  
  
            SSLContext context = SSLContext.getInstance("TLS");  
            context.init(null, new X509TrustManager[] { new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
  
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
  
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];  
                }
            } }, new SecureRandom());  
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            // e.printStackTrace();  
        }
    }  

}
